package com.example.vspm.controller;

import com.example.vspm.entity.ExecutiveReport;
import com.example.vspm.entity.MeetingPlace;
import com.example.vspm.entity.Person;
import com.example.vspm.entity.TaskForm;
import com.example.vspm.service.*;
import com.example.vspm.util.StateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author LCL
 * @date 2022/3/12
 * @desc
 */
@Controller
@RequestMapping("/receiver")
public class ReceiverController {
    @Autowired
    TaskFormService taskFormService;

    @Autowired
    StateUtil stateUtil;

    @Autowired
    HttpServletRequest request;//通过注解获取一个request,获取session

    @Autowired
    MeetingPlaceService meetingPlaceService;

    @Autowired
    SendTableService sendTableService;

    @Autowired
    ReceiverService receiverService;

    @Autowired
    ExecutiveReportService executiveReportService;


    //接单员查看工单
    @RequestMapping("/listTaskFormByPid3")
    public String listTaskFormByPid3(Model model){
        Person person=(Person) request.getSession().getAttribute("person");
        List<TaskForm> taskForm = taskFormService.listTaskFormByPid3(person.getTid());
        model.addAttribute("listTaskForms",taskForm);
        return "taskForm/listTaskForm";
    }


    //根据前端传过来工单ID查看工单详情页,方便显示会场以及接单人员的挑选,评价....
    @RequestMapping("/listTaskFormByTid/{tid}")
    public String listTaskFormByTid(@PathVariable("tid") int tid, Model model){
        List<TaskForm> taskForms = taskFormService.listTaskFormByTid(tid);
        model.addAttribute("listTaskForms",taskForms);
        //查看工单对应的会场
        List<MeetingPlace> meetingPlaces=meetingPlaceService.listMeetingPlaceByTaid(tid);
        model.addAttribute("listMeetingPlaces",meetingPlaces);
        return "工单详情页(有会场,有选择接单人员按钮)";
    }


    //接单员拒绝的话,将工单tid传过来
    @RequestMapping("/refuseTaskForm/{tid}")
    public String refuseTaskForm(@PathVariable("tid") int tid){
        Person person=(Person) request.getSession().getAttribute("person");
        taskFormService.updateState(stateUtil.taskFormStateSwitch("2"),tid);
//        taskFormService.updateTaskFormPID3(person.getTid(),tid);
        receiverService.updateRefuseCount(person.getTid());
        receiverService.updateScoreCount(person.getTid());
        sendTableService.updatePersonnelStatus(stateUtil.sendTableStatusSwitch("2"),tid,person.getTid());
        return "taskForm/listTaskForm";
    }

    //接单员接受后,将工单tid传过来
    @RequestMapping("/acceptTaskForm/{tid}")
    public String acceptTaskForm(@PathVariable("tid") int tid){
        //此时还要更新相应的工单状态,进行中
        Person person=(Person) request.getSession().getAttribute("person");
        taskFormService.updateState(stateUtil.taskFormStateSwitch("4"),tid);
        sendTableService.updatePersonnelStatus(stateUtil.sendTableStatusSwitch("1"),tid,person.getTid());
        return "taskForm/listTaskForm";
    }


    @RequestMapping("/add")
    public String add(){
        return "填写执行工单页面";
    }


    //接单员接受后,要进行执行工单的填写,前端要将工单tid传过来
    @RequestMapping("/addExecutiveReport/{tid}")
    public String addExecutiveReport(@PathVariable("tid") int tid, ExecutiveReport executiveReport,Model model){
        executiveReportService.addExecutiveReport(tid,executiveReport);
        //此时还要更新相应的工单状态,待评价
        taskFormService.updateState(stateUtil.taskFormStateSwitch("6"),tid);
        //跳转到执行工单生成的页面
        return listExecutiveReportByTid(tid,model);
    }

    //根据前端传过来工单id查看执行报告,前端for循环
    @RequestMapping("/listExecutiveReportByTid/{tid}")
    public String listExecutiveReportByTid(@PathVariable("tid") int tid, Model model){
        List<ExecutiveReport> executiveReports = executiveReportService.listExecutiveReportByTid(tid);
        model.addAttribute("listExecutiveReports",executiveReports);
        return "展示执行报告页面";
    }











}
